package cn.zwk.product.service.impl;

import cn.zwk.common.constant.ProductConstant;
import cn.zwk.product.entity.AttrAttrgroupRelationEntity;
import cn.zwk.product.entity.AttrEntity;
import cn.zwk.product.entity.CategoryEntity;
import cn.zwk.product.service.AttrAttrgroupRelationService;
import cn.zwk.product.service.AttrService;
import cn.zwk.product.vo.GroupWithAttrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zwk.common.utils.PageUtils;
import cn.zwk.common.utils.Query;

import cn.zwk.product.dao.AttrGroupDao;
import cn.zwk.product.entity.AttrGroupEntity;
import cn.zwk.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> term = new LambdaQueryWrapper<>();
        if (catelogId!=null&&catelogId!=0) {
            term.eq(AttrGroupEntity::getCatelogId,catelogId);
        }
        if (params.get("key")!=null) {
            term.and((obj) -> {
                obj.eq(AttrGroupEntity::getAttrGroupId, params.get("key")).or().like(AttrGroupEntity::getAttrGroupName, params.get("key"));
            });
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                term
        );
        return new PageUtils(page);
    }
    /**
     * 查询分类关联的属性
     *
     * @param attrgroupId
     * @return
     */
    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> list = relationService.
                list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        if (list == null || list.size() <= 0) {
            return null;
        }
        List<Long> attrIds = list.stream().map(attr -> attr.getAttrId()).collect(Collectors.toList());
        return attrService.listByIds(attrIds);
    }

    /**
     * 查询此分租没有关联的属性
     * 这里用子查询更好一些
     * @param attrgroupId
     * @return
     */
    @Override
    public PageUtils getNoRelationAttr(Long attrgroupId, Map<String, Object> params) {

        AttrGroupEntity attrGroupEntity = this.getById(attrgroupId);
        List<AttrAttrgroupRelationEntity> list = this.relationService.list();
        //所有分组关联的attr
        List<Long> attrIds = list.stream().map(relation -> relation.getAttrId()).collect(Collectors.toList());
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
        String key= (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and(w->{
                w.eq(AttrEntity::getAttrId,key).or().like(AttrEntity::getAttrName,key);
            });
        }
        wrapper.eq(AttrEntity::getCatelogId, attrGroupEntity.getCatelogId())
                .ne(AttrEntity::getAttrType, ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode())
                .notIn(AttrEntity::getAttrId, attrIds);
        IPage<AttrEntity> resPage = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);
        return new PageUtils(resPage);
    }

    @Override
    @Transactional
    public boolean removeRelation(List<AttrAttrgroupRelationEntity> relationEntity) {
        if (relationEntity==null||relationEntity.size()<=0){
            return false;
        }
        this.baseMapper.removeRelation(relationEntity);
        return true;
    }

    @Override
    public List<GroupWithAttrVo> groupWithAttrByCatId(Long catelogId) {
        List<AttrGroupEntity> groupList = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        List<GroupWithAttrVo> vos = groupList.stream().map(group -> {
            GroupWithAttrVo vo = new GroupWithAttrVo();
            BeanUtils.copyProperties(group, vo);
            List<AttrEntity> list = this.getRelationAttr(group.getAttrGroupId());
            vo.setAttrs(list);
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }
}
